<template>
  <div >
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 19px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
    </el-breadcrumb>
  <el-divider></el-divider>
    <el-table :data="tableData" border style="width: 100%;">
      <el-table-column prop="id" label="id" width="50px" align="center"></el-table-column>
      <el-table-column prop="username" label="用户名" width="120" align="center"></el-table-column>
      <el-table-column prop="nickname" label="昵称" width="100px" header-align="center"></el-table-column>
<!--      <el-table-column prop="avatar" label="头像" width="250px" header-align="center"></el-table-column>-->
      <el-table-column prop="phone" label="手机号" width="150" align="center" ></el-table-column>
      <el-table-column prop="email" label="邮箱" width="150" header-align="center"></el-table-column>
      <el-table-column prop="enable" label="=是否启用" width="80px">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.enable"
              @change="updateEnable(scope.row)"
              :active-value="1"
              :inactive-value="0"
              :disabled="scope.row.id == 1"
              active-color="#13ce66"
              inactive-color="#aaaa">
          </el-switch>
        </template>

      </el-table-column>
      <el-table-column prop="lastLoginIp" label="最后登录的ip" width="100px"></el-table-column>
      <el-table-column prop="loginCount" label="累计登录次数" width="50px"></el-table-column>
      <el-table-column prop="gmtLastLogin" label="最后登录时间" width="100px"></el-table-column>
      <el-table-column prop="positiveCommentCount" label="买家好评数量总和" width="100px"></el-table-column>


      <el-table-column  label="操作" width="100px" header-align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini" @click="handleEdit(scope.row)"
          :disabled="scope.row.id == 1"
          ></el-button>
          <el-button type="danger" @click="openDeleteConfirm(scope.row)" icon="el-icon-delete"
                     circle size="mini"
                     :disabled="scope.row.id == 1"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData:[]
    }
  },
  methods: {

    updateEnable(admin){
      let ableText=['禁用','启用'];
      let url = 'http://localhost:9091/admins/'+admin.id;
      if(admin.enable == 1){
        url = url+'/enable'
      }else {
        url = url+'/disable'
      }
      this.axios.post(url).then((response) =>{
        let responseBody = response.data;
        if( responseBody.state==20000  ){
          let message='将管理员【'+admin.username+'】已经【'+ableText[admin.enable]+'】成功';
          this.$message({
            showClose: true,
            message: message,
            type: 'success'
          });
        }else {
          this.$message.error(responseBody.message)
        }
        if(responseBody.state == 40400){
          this.loadAdminList();
        }
      })
    },
    handleEdit(admin){
      let message= '您正在尝试编辑【' + admin.id + '-' + admin.username + '】的管理员详情，改你大爷的改';
      this.$alert(message,'提示',{
        confirmButtonText:'确定'
      });
    },
    handleDelete(admin){
      let url = 'http://localhost:9091/admins/'+admin.id+'/delete'
      this.axios.post(url).then((response) =>{
        let responseBody = response.data;
        if(responseBody.state != 20000){
          this.$message.error(responseBody.message)
        }
        if(responseBody.state == 40400|| responseBody.state==20000){
          this.loadAdminList();
        }
      })
      console.log('删除的id='+ admin.id)

    },
    openDeleteConfirm(admin) {
      this.$confirm('此操作将永久删除【'+admin.username+'】品牌, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(admin)
        this.$message.success("删除成功")
      }).catch(() => {});
    },

    loadAdminList(){
      let url = "http://localhost:9091/admins";
      console.log('url=' + url);
      this.axios.get(url).then((response) =>{
        let responseBody = response.data;
        this.tableData = responseBody.data;
      })
    },


  },
  mounted() {
    console.log("mounted")
    this.loadAdminList();
  }

}
</script>